package com.itcast.mobileController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itcast.health.entity.Result;
import com.itcast.health.pojo.Member;
import com.itcast.health.pojo.RelativeInfoVo;
import com.itcast.health.utils.RedisUtils;
import com.itcast.service.RelativeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: libin
 * @date: 2019/12/3 11:24
 * @version: 1.0
 * @describe:
 */
@RequestMapping("/relative")
@RestController
public class RelativeController {

    @Reference
    RelativeService relativeService;

    @Autowired
    RedisUtils redisUtils;

    /**
     * 添加联系人
     * @param relativeInfoVo
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody RelativeInfoVo relativeInfoVo) {
        try {
            //获取用户的id
            String token = relativeInfoVo.getToken();
            String s = redisUtils.get(token);
            Member member = JSON.parseObject(s, Member.class);
            Integer id = member.getId();
            //将用户的id和其联系人的信息,进行储存
            Result result = relativeService.add(id,relativeInfoVo);
            //返回数据
            return result;
        } catch (Exception e) {
           return new Result(false,"添加联系人失败");
        }

    }

    /**
     * 查询所有联系人
     * @param token
     * @return
     */
    @RequestMapping("/findAllRelative")
    public Result findAllRelative(String token) {
            //获取用户的id
            String s = redisUtils.get(token);
            Member member = JSON.parseObject(s, Member.class);
            Integer id = member.getId();
            //查询所有联系人
            List<Member> members = relativeService.findAllRelative(id);
            //返回数据
            return new Result(true,"",members);
    }

    /**
     * 根据id查询联系人
     * @param rid
     * @return
     */
    @RequestMapping("/findRelativeById")
    public Result findRelativeById( Integer rid) {

        //根据id查询联系人
        Member member = relativeService.findRelativeById(rid);
        //返回数据
        return new Result(true,"",member);
    }





}
